#!/usr/bin/python
# -*- coding: UTF-8 -*-

from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
import time


class BMC_SSH_009(Case):
    """
        Name:
            BMC_SSH_009
        Label:
            重启BMC测试
        PreCondition:
            1、BMC正常运行
        TestStep:
            1.SSH登录BMC
            2.重启BMC：iBMC:/->ipmcset -d reset，弹出对话框，选择Y
            3.重新SSH连接BMC，有结果A）
        ExpectedResult:
            A) BMC连接成功
        Parameters:

        Remark:

    """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.os_host = self.device_dut.get_host(host_type=HostType.HostOS)
        self.cli_api = self.device_dut.get_api(ns_name="BmcCli")
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)
        self.bmc_ip = self.bmc_host.local_ip

    # 测试步骤
    def procedure(self):
        self.log_step("1、执行ipmcget -d reset命令，重启BMC")
        reset_info = self.cli_api.cli_restart_ibmc()
        if reset_info == None:
            raise Exception("重启BMC失败")
        self.log_info(f"重启BMC命令的返回值：{reset_info}")

        self.log_info(f"等待BMC完成重启......")
        time.sleep(60)
        ping_times = 30
        for _ in range(ping_times):
            ping_result = self.os_host.ping_address(self.bmc_ip, 4)
            if ping_result == None:
                raise Exception("ping BMC IP失败")
            if ping_result == True:
                self.log_info(f"BMC已经完成重启")
                break
            time.sleep(10)
        self.log_step("2、登录BMC，查看版本信息")
        device_version = self.cli_api.cli_query_device_version()
        if device_version == None:
            raise Exception("获取版本信息失败")
        self.log_info(f"BMC版本信息为：{device_version}")
    # 测试结束后恢复环境
    def post_test_case(self):
        pass